package com.jc.service.user;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jc.cds.model.CustomUser;
import com.jc.common.util.CheckNullUtils;
import com.jc.ds.base.service.BaseServiceImpl;
import com.jc.ds.mapper.BaseCustomMapper;
import com.jc.ds.mapper.UserMapper;
import com.jc.ds.model.Role;
import com.jc.ds.model.User;
import com.jc.ds.model.UserRole;
import com.jc.service.role.RoleService;
import com.jc.service.userrole.UserRoleService;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@SuppressWarnings("all")
@Service
public class UserServiceImpl extends BaseServiceImpl<User, CustomUser, Long> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleService roleService;

    @Autowired
    private UserRoleService userRoleService;

    @Override

    public BaseCustomMapper<CustomUser, Long> getCustomMapper() {
        return null;
    }


    @Override
    public User doLoginCheck(String userName, String passWord) {

//		this.queryAll();


        return null;
    }

    /**
     * 新增
     */
    @Transactional
    @Override
    public Boolean userAdd(String userName, String passWord, String realName) {

        User user = new User();

        user.setUserName(userName);
        user.setName(realName);
        user.setCreateTime(new Date());

        user.setIsDelete(0);
        user.setIsEnable("normal");

        String salt = "1";
        ByteSource salt1 = ByteSource.Util.bytes(userName + 1);
        String s = new SimpleHash("MD5", passWord, salt1, 2).toString();
        user.setPassWord(s);
        user.setSalt(salt);
        int insert = this.userMapper.insert(user);
        Boolean flag = false;
        if (insert == 1) {
            flag = true;
        }
        return flag;
    }


    /**
     * 查询用户
     */
    @Override
    public Map<String, Object> userQuery(String userName, String name, String start, String limit) {
        Map<String, Object> result = new HashMap<String, Object>();
        User user = new User();
        if (!"null".equals(userName) && userName != null && !"".equals(userName) && !"userName".equals(userName)) {

            user.setUserName(userName);
        }
        if (!"null".equals(name) && name != null && !"".equals(name) && !"name".equals(name)) {

            user.setName(name);
        }

        user.setIsDelete(0);
        if (!CheckNullUtils.isNull(start) || !CheckNullUtils.isNull(limit)) {
            Page<Object> objects = PageHelper.startPage(Integer.valueOf(start), Integer.parseInt(limit));
        }
//
        List<User> users = super.queryLikeAll(user);
        int count1 = super.queryAllCount(user);
       // int count = users.size();

        result.put("data", users);
        result.put("code", "10000");
        result.put("count", count1);
        return result;
    }

    /**
     * 查询用户
     */
    @Override
    public Map<String, Object> userQueryByName(String name) {
        Map<String, Object> result = new HashMap<String, Object>();
        User user = new User();

        String naa = name.substring(1, name.length() - 1);
        System.out.println("naa = " + naa);
        user.setUserName(naa);

        List<User> users = this.userMapper.selectLikeAll(user);
        int count = users.size();

        result.put("data", users);
        result.put("code", "10000");
        result.put("count", count);
        return result;
    }

    /**
     * 修改用户
     */

    @Override
    public int userUpdate(String id, String userName, String realName, String passWord) {
        User user = new User();
        user.setId(Long.valueOf(id));
        if (!"null".equals(userName) && userName != null) {
            user.setUserName(userName);
        }
        if (!"null".equals(realName) && realName != null) {

            user.setName(realName);
        }
        if (!"null".equals(passWord) && passWord != null) {

            String salt = "1";
            ByteSource salt1 = ByteSource.Util.bytes(userName + 1);
            String s = new SimpleHash("MD5", passWord, salt1, 2).toString();
            user.setPassWord(s);
        }


        return this.userMapper.updateByPrimaryKeySelective(user);


    }

    /***
     * 删除用户
     *
     */
    @Override
    public int userDelete(int id) {

        User user = new User();
        user.setId(Long.valueOf(id));
     /*   user.setIsDelete(1);
        return this.userMapper.updateByPrimaryKeySelective(user);*/
        List<UserRole> userRoles = this.userRoleService.queryAll(new UserRole() {{
            setUserId(Long.valueOf(id));
        }});
        for (UserRole userRole : userRoles) {
            this.userRoleService.remove(userRole.getId());
        }

        return this.userMapper.deleteByPrimaryKey(Long.valueOf(id));


    }

    /***
     * 所有用户名
     */

    @Override
    public Map<String, Object> selectAllUserName() {

        Map<String, Object> result = new HashMap<>();
        User user = new User();
        user.setIsDelete(0);

        List<User> users = this.userMapper.selectAll(user);
        ArrayList<String> userNames = new ArrayList<>();
        for (User user1 : users) {
            userNames.add(user1.getUserName());
        }
        result.put("data", userNames);
        result.put("code", "10000");

        return result;
    }

    /***
     * 所有真实名
     */
    @Override
    public Map<String, Object> selectAllName() {

        Map<String, Object> result = new HashMap<>();
        User user = new User();
        user.setIsDelete(0);
        List<User> users = this.userMapper.selectAll(user);
        ArrayList<String> names = new ArrayList<>();
        for (User user1 : users) {
            names.add(user1.getName());
        }
        result.put("data", names);
        result.put("code", "10000");

        return result;
    }

    /***
     *根据用户id查询用户
     */
    @Override
    public Map<String, Object> queryUserById(Map<String, Object> params) {

        Map<String, Object> result = new HashMap<>();
        String id = String.valueOf(params.get("id"));

        if (CheckNullUtils.isNull(id)) {
            result.put("msg", "该用户暂时无法编辑，请检查");
            result.put("code", "4000");
            return result;
        }
        List<User> users = this.userMapper.selectAll(new User() {{
            setId(Long.valueOf(id));
        }});

        if (users.size() > 1 || users.size() == 0) {
            result.put("msg", "该用户存在多个或者不存在，亲稍后再试试");
            result.put("code", "4000");
            return result;
        }
        User user = users.get(0);


        result.put("data", user);
        result.put("code", "10000");
        return result;
    }


    /***
     *根据用户ids查询用户
     */
    @Override
    public Map<String, Object> queryUserByIds(Map<String, Object> params) {
        Map<String, Object> result = new HashMap<>();
        String idss = String.valueOf(params.get("ids"));

        if (CheckNullUtils.isNull(idss)) {
            result.put("msg", "该用户暂时无法编辑，请检查");
            result.put("code", "4000");
            return result;
        }
        List<Integer> ids = (List<Integer>) JSONObject.parse(String.valueOf(params.get("ids")));

        ArrayList<User> objects = new ArrayList<>();
        for (Integer id : ids) {

            List<User> users = this.userMapper.selectAll(new User() {{
                setId(Long.valueOf(id));
            }});
            User user = users.get(0);
            objects.add(user);
        }
        ;

        result.put("data", objects);
        result.put("code", "10000");
        result.put("count", objects.size());
        return result;

    }

    /***
     *用户到角色
     */
    @Override
    public Map<String, Object> userToRole(Map<String, Object> params) {
        Map<String, Object> result = new HashMap<>();
        if (CheckNullUtils.isNull(String.valueOf(params.get("userName")))) {
            result.put("code", 6666);
            result.put("data", "fail");
            result.put("msg", "没有接收到userName");
            return result;
        }
        if (CheckNullUtils.isNull(String.valueOf(params.get("roleName")))) {
            result.put("code", 6666);
            result.put("data", "fail");
            result.put("msg", "没有接收到roleName");
            return result;
        }
        String roleName = String.valueOf(params.get("roleName"));
        String userName = String.valueOf(params.get("userName"));
        int add = this.userRoleService.add(new UserRole() {{
            setUserId(Long.valueOf(userName));
            setRoleId(Long.valueOf(roleName));
        }}, false);
        if (add != 1) {
            result.put("data", "失败");
            result.put("msg", "失败");
            result.put("code", "6666");
            return result;
        }
        result.put("data", "成功");
        result.put("msg", "成功");
        result.put("code", "10000");
        return result;
    }
}
